package com.erdos.common.web.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.erdos.common.service.AppServiceHelper;
import com.erdos.common.util.AppContext;


/**
 * ---------------------------------------------------------------------------------
 Confidential and Proprietary                                                                
 Copyright 2008 By                                                                                     
 SGAI & Hewlett-Packard Development Company, L.P. 	              
 All Rights Reserved                                                                                  

Project Name : SGAI  MES                                                                                                                                       
 Class Name   : ApplicationContextListener.java    
 Package      : com.hp.common.web.listener                                                                   
 $Id: ApplicationContextListener.java 9124 2012-03-08 03:00:56Z hubin $       :                                                                    

 */

public class ApplicationContextListener implements ServletContextListener {
	
	private static final Log logger = LogFactory.getLog(ApplicationContextListener.class);

    public void contextInitialized(ServletContextEvent event) {
        try {
        	ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
            ServletContext sc = event.getServletContext();
        	String appName=sc.getServletContextName();
        	logger.info("["+appName+"] init context ...");
        	AppServiceHelper.setApplicationContext(applicationContext);
        	logger.info(" - Put Spring Context to AppServiceHelper");
            String facility = sc.getInitParameter("facility");
            AppContext.addParam(AppContext.FACILITY, facility);
            logger.info(" - Put FACILITY["+facility+"] to AppContext");
            logger.info("["+appName+"] init context[done]");
        } catch (Exception e) {
            logger.error("error detail:",e);
        }
    }

    public void contextDestroyed(ServletContextEvent arg0) {

    }


}
